package com.example.schoolhelp.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.schoolhelp.R;
import com.example.schoolhelp.base.BaseActivity;
import com.example.schoolhelp.bean.PostBean;
import com.example.schoolhelp.util.ImageFromAssets;
import com.example.schoolhelp.util.MyImageAddr;

/**
 * Created by user on 2017/7/21.
 */

public class RewordDetailActivity extends BaseActivity implements View.OnClickListener {
    Toolbar mToolbar;
    CollapsingToolbarLayout mCollapsingToolbarLayout;
    AppBarLayout mAppBar;
    ImageView iv_rewordPic;
    TextView tv_rewordContent;
    TextView mTvTitle;
    PostBean postBean;
    private int position;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reword_detail);
        initData();
        initView();
        initViewData();
        initEvent();


    }

    private void initData() {
        postBean = (PostBean) getIntent().getSerializableExtra("data");
        position = getIntent().getIntExtra("position", 0);
    }

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.title_tv);
        mAppBar = (AppBarLayout) findViewById(R.id.reword_detail_appBar);
        tv_rewordContent = (TextView) findViewById(R.id.reword_detail_tv_content);
        iv_rewordPic = (ImageView) findViewById(R.id.reword_detail_pic);
        mToolbar = (Toolbar) findViewById(R.id.reword_detail_toolbar);
        //使用CollapsingToolbarLayout必须把title设置到CollapsingToolbarLayout上，设置到Toolbar上则不会显示
        mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
    }

    private void initViewData() {
        setSupportActionBar(mToolbar);
        mTvTitle.setText(getString(R.string.reword_detail));
        //通过CollapsingToolbarLayout修改字体颜色
//        mCollapsingToolbarLayout.setExpandedTitleColor(Color.TRANSPARENT);//设置还没收缩时状态下字体颜色
//        mCollapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);
        tv_rewordContent.setText(postBean.getDescription());
        iv_rewordPic.setImageBitmap(ImageFromAssets.getAssetsImag(this, MyImageAddr.simpleAddr(position % 10)));

        ViewCompat.setTransitionName(iv_rewordPic, "iv");
        ViewCompat.setTransitionName(tv_rewordContent, "tv");
    }

    private void initEvent() {
        findViewById(R.id.title_left_layout).setOnClickListener(this);


        mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int scrollRangle = appBarLayout.getTotalScrollRange();
                //设置滑动监听
                if (verticalOffset == 0) {
                    //完全展开
                } else if (Math.abs(verticalOffset) >= scrollRangle) {
                    //折叠
                } else {
                    //中间
                    float alpha = Math.abs(Math.round(1.0f * verticalOffset / scrollRangle) * 10) / 10;
                    mTvTitle.setAlpha(alpha);
                }

            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_layout:
                finish();
                break;
        }
    }
}
